"""
自定义身份认证,可以认证用户名和手机
"""
from django.contrib.auth.backends import ModelBackend
from users.models import User


class UsernameMobileBackendView(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            try:
                user = User.objects.get(mobile=username)
            except User.DoesNotExist:
                return None
        if not user.check_password(password):
            return None

        return user




